package info.niwota.frames;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class Dialogs {

	public static class ItemInfo {
		public Drawable icon;
		public int iconRes;
		public String title;
		public Intent intent;
		public Object object;
		
		public Drawable getIcon() {
			return icon;
		}
		public void setIcon(Drawable icon) {
			this.icon = icon;
		}
		public int getIconRes() {
			return iconRes;
		}
		public void setIconRes(int iconRes) {
			this.iconRes = iconRes;
		}
		public String getTitle() {
			return title;
		}
		public void setTitle(String title) {
			this.title = title;
		}
		public Intent getIntent() {
			return intent;
		}
		public void setIntent(Intent intent) {
			this.intent = intent;
		}
		public Object getObject() {
			return object;
		}
		public void setObject(Object object) {
			this.object = object;
		}
	}

	protected static final String TAG = "Dialogs";


//	public static ProgressDialog createProgressDialog(final Context ctx) {
//		ProgressDialog pd = new ProgressDialog(ctx);
//		pd.setMessage(ctx.getText(R.string.text_please_wait));
//		pd.setIndeterminate(true);
//		pd.setCancelable(false);
//		//
//		return pd;
//	}
	
	public static AlertDialog create(final Context ctx, final CharSequence title,
			final ItemListAdapter adapter) {
		final AlertDialog.Builder b = new AlertDialog.Builder(ctx);
		b.setTitle(title);
		b.setIcon(R.drawable.ic_dialog_menu_generic);
		b.setInverseBackgroundForced(true);
		//	
		b.setAdapter(adapter, new OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				if (Debug.DEBUG) {
					Log.d(TAG, "onClick " + which);
				}
				adapter.onClick(dialog, which);
				dialog.dismiss();
			}
		});

		b.setCancelable(true);
		final AlertDialog d = b.create();
		return d;
	}

	public static abstract class ItemListAdapter extends BaseAdapter implements OnClickListener {
		private LayoutInflater inflater;

		protected List<ItemInfo> list;

		protected Context context;

		public ItemListAdapter(Context context) {
			this.context = context;
			init();
		}
		
		private void init() {
			inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			list = new ArrayList<ItemInfo>();
		}
		
		public void reload() {
			list.clear();
			loadItems(context, list);
			notifyDataSetChanged();
		}

		
		public Intent intentForPosition(int position) {
			if (list == null) {
				return null;
			}
			ItemInfo info = list.get(position);
			return info.getIntent();
		}

		public int getCount() {
			return list != null ? list.size() : 0;
		}

		public ItemInfo getItem(int position) {
			if (list == null) {
				return null;
			}
			ItemInfo info = list.get(position);
			return info;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			View view;
			if (convertView == null) {
				view = inflater.inflate(R.layout.list_item, parent, false);
			} else {
				view = convertView;
			}
			bindView(view, list.get(position));
			return view;
		}

		private final void bindView(View view, ItemInfo info) {
			TextView text = (TextView) view.findViewById(R.id.line1);

			ImageView icon = (ImageView) view.findViewById(R.id.icon);
			text.setText(info.getTitle());
			int res = info.getIconRes();
			if (res > 0) {
				icon.setImageResource(res);
			} else {
				Drawable d = info.getIcon();
				icon.setImageDrawable(d);
			}
		}

		abstract public void loadItems(Context context, List<ItemInfo> list);

		public void onClick(DialogInterface dialog, int which) {
		}
	}
}
